package com.rarebot.script.randoms;

import com.rarebot.script.Random;
import com.rarebot.script.ScriptManifest;
import com.rarebot.script.wrappers.RSObject;

@ScriptManifest(authors = {"Braco22", "Swipe, Poxer"}, name = "FirstTimeDeath", version = 1.2)
public class FirstTimeDeath extends Random {
	boolean talkDone = false;

	public boolean activateCondition() {
		if (!game.isLoggedIn()) {
			return false;
		} else if (objects.getNearest(45802) != null && npcs.getNearest(7910) != null) {
			sleep(random(2000, 3000));
			return (objects.getNearest(45802) != null && npcs.getNearest(7910) != null);
		}
		return false;
	}

	@Override
	protected int loop() {
		if (!activateCondition()) {
			return -1;
		}
		if (interfaces.canContinue()) {
			if (interfaces.getChatInterface().isValid()) {
				interfaces.getChatInterface().getComponent(20).doClick();
			} else if (interfaces.getChatInterface().getComponent(3).getText().contains("Goodbye!")) {
				interfaces.getChatInterface().getComponent(3).doClick();
				talkDone = true;
			} else if (interfaces.getChatInterface().isValid()) {
				interfaces.getChatInterface().getComponent(3).doClick();
			}
			sleep(random(200, 600));
		}
		if (!talkDone && !interfaces.getChatInterface().isValid()) {
			if (objects.getNearest(45802) != null) {
				camera.turnTo(objects.getNearest(45802));
				objects.getNearest(45802).doClick();
				sleep(random(500, 900));
			}
		}
		if (talkDone) {//We have finished talking to reaper
			final RSObject portal = objects.getNearest(45803);//get portal
			camera.turnTo(portal);//turn
			if (calc.distanceTo(portal) <= 4) {
				portal.interact("Enter");//Leave
				return random(1000, 1200);
			} else {
				walking.walkTileMM(portal.getLocation()); //walk portal
				return random(1000, 1300);
			}
		}
		return random(200, 400);
	}
}
